package org.celery.modules.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.celery.modules.merchant.entity.MerchantDesk;
import org.celery.modules.merchant.vo.MerchantDeskVO;
import org.celery.modules.merchant.wrapper.MerchantDeskWrapper;
import org.celery.modules.merchant.service.IMerchantDeskService;
import org.springblade.core.boot.ctrl.BladeController;

/**
 * 商户桌台表. 控制器
 *
 * @author Celery
 * @since 2021-11-07
 */
@RestController
@AllArgsConstructor
@RequestMapping("celery-merchant/merchant-desk")
@Api(value = "商户桌台表.", tags = "商户桌台表.接口")
public class MerchantDeskController extends BladeController {

	private final IMerchantDeskService merchantDeskService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入merchantDesk")
	public R<MerchantDeskVO> detail(MerchantDesk merchantDesk) {
		MerchantDesk detail = merchantDeskService.getOne(Condition.getQueryWrapper(merchantDesk));
		return R.data(MerchantDeskWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 商户桌台表.
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入merchantDesk")
	public R<IPage<MerchantDeskVO>> list(MerchantDesk merchantDesk, Query query, BladeUser bladeUser) {
		QueryWrapper<MerchantDesk> queryWrapper = Condition.getQueryWrapper(merchantDesk);
		if (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) {
			queryWrapper.lambda().eq(MerchantDesk::getTenantId, bladeUser.getTenantId());
		}
		queryWrapper.lambda().orderByAsc(MerchantDesk::getTenantId);
		queryWrapper.lambda().orderByAsc(MerchantDesk::getSort);
		IPage<MerchantDesk> pages = merchantDeskService.page(Condition.getPage(query), queryWrapper);
		return R.data(MerchantDeskWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页 商户桌台表.
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入merchantDesk")
	public R<IPage<MerchantDeskVO>> page(MerchantDeskVO merchantDesk, Query query) {
		IPage<MerchantDeskVO> pages = merchantDeskService.selectMerchantDeskPage(Condition.getPage(query), merchantDesk);
		return R.data(pages);
	}

	/**
	 * 新增 商户桌台表.
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入merchantDesk")
	public R save(@Valid @RequestBody MerchantDesk merchantDesk) {
		return R.status(merchantDeskService.save(merchantDesk));
	}

	/**
	 * 修改 商户桌台表.
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入merchantDesk")
	public R update(@Valid @RequestBody MerchantDesk merchantDesk) {
		return R.status(merchantDeskService.updateById(merchantDesk));
	}

	/**
	 * 新增或修改 商户桌台表.
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入merchantDesk")
	public R submit(@Valid @RequestBody MerchantDesk merchantDesk, BladeUser bladeUser) {
		if (Func.isEmpty(merchantDesk.getId())) {
			merchantDesk.setTenantId(bladeUser.getTenantId());
		}
		return R.status(merchantDeskService.saveOrUpdate(merchantDesk));
	}

	
	/**
	 * 删除 商户桌台表.
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(merchantDeskService.deleteLogic(Func.toLongList(ids)));
	}

	
}
